package com.df.ez_game.server.gateway;

import com.df.ez_game.server.*;
import com.df.ez_game.server.gateway.tcp.GatewayTcpNettyServerSettings;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class GateWayServerNode{
    //配置信息
    GateWaySettings settings = new GateWaySettings();
    //对外的服务器
    NettyServer nettyServer;
    //broker管理器
    BrokerManager brokerManager = BrokerManager.getInstance();
    //用户管理器
    UserManager userManager = new UserManager();
    //请求元数据



    void runServer(){

//        nettyServer.startUp(channel -> {
//            log.info("网关服务启动成功！端口："+nettyServer.getPort());
//        },channel -> {
//            log.info("网关服务启动失败！端口："+nettyServer.getPort());
//        });
    }



    public void startup(){
        //检查配置
        this.preCheck();
        //收集网关的controller信息

        //启动连接broker的客户端，连接并注册

        //启动对外服务
        this.runServer();
    }

    private void preCheck() {
        //检测setting
        if(settings.getPort()==null){
            settings.setPort(6666);
        }
        if(settings.getBrokerIp()==null || settings.getBrokerPort()==null){
            log.error("broker的ip和端口未设置，无法注册到broker");
        }
        if(settings.getNetProtoc()==null){
            log.warn("未配置网关网络协议，使用默认tcp协议！");
            settings.setNetProtoc(NetProtocType.TCP.getValue());
        }
        boolean protocSupport = false;
        for (NetProtocType value : NetProtocType.values()) {
            if(value.getValue() == settings.getNetProtoc()){
                protocSupport = true;
                break;
            }
        }
        if(!protocSupport){
            throw new RuntimeException("网关未支持的对外协议值："+settings.getNetProtoc()+"  目前只支持TCP(1);WEBSOCKET(2);UDP(3)");
        }
        if(nettyServer==null){
            nettyServer = new NettyServer();
//            nettyServer.setPort(settings.getPort());
            NettyServerSettings nettySetting = new GatewayTcpNettyServerSettings();
            nettyServer.setSettings(nettySetting);
        }
    }
}
